#ifndef __UART_HPP__
#define __UART_HPP__

#include <iostream>
#include <functional>
#include <string>
#include <vector>
#include <stdexcept>
#include <thread>
#include <atomic>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <pthread.h>

class uart{
public:
    using data_callback_t = std::function<void(const std::vector<uint8_t>&)>; 
    uart(const std::string &port_name,int baud_rate=B115200,bool ctsrts = false);
    ~uart();

    void open_port();
    void close_port();
    void write_data(const std::vector<uint8_t> &data);
    void set_data_callback(data_callback_t callback);


private:
    std::string port_name;
    int baud_rate;
    bool ctsrts;
    int fd;
    termios tty;
    std::thread read_thread;
    std::atomic<bool> running;
    data_callback_t data_callback; 

    void configure_port();
    void read_loop();
    

};




#endif //__UART_HPP__
